package com.holyn.guofang.presenter.distinguish;

import com.apkfuns.logutils.LogUtils;
import com.holyn.guofang.MyApplication;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.RetrofitManager;
import com.holyn.guofang.model.webhttp.observable.BrandObservable;
import com.holyn.guofang.model.webhttp.observable.CardObservable;
import com.holyn.guofang.model.webhttp.observable.UserLoginedObservable;
import com.holyn.guofang.model.webhttp.wo.BrandItemInfoWo;
import com.holyn.guofang.model.webhttp.wo.GraphTransWo;
import com.holyn.guofang.model.webhttp.wo.ImageWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.TaskIDWo;
import com.holyn.guofang.model.webhttp.wo.UserCardListWo;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.luck.picture.lib.compress.Luban;
import com.vondear.rxtools.RxTimeUtils;

import java.io.File;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Holyn on 2017/6/25.
 */

public class DistinguishPresenter extends DistinguishContract.Presenter {
    private final BrandObservable brandObservable;

    private DistinguishContract.CommonView commonView = null;
    private DistinguishContract.BrandItemView brandItemView = null;
    private DistinguishContract.UploadView uploadView = null;
    private DistinguishContract.ResposeGraphTransListener resposeGraphTransListener = null;

    private UserLoginedObservable loginedObservable;
    private CardObservable cardObservable;

    public DistinguishPresenter(BaseView baseView) {
        super(baseView);
        this.brandObservable = RetrofitManager.getCommonObservable(BrandObservable.class);
        loginedObservable = RetrofitManager.getCommonObservable(UserLoginedObservable.class);
        cardObservable = RetrofitManager.getCommonObservable(CardObservable.class);
    }

    public void addCommonViewListener(DistinguishContract.CommonView commonView) {
        this.commonView = commonView;
    }

    @Override
    public void addBrandItemViewListener(DistinguishContract.BrandItemView brandItemView) {
        this.brandItemView = brandItemView;
    }

    @Override
    public void addUploadViewListener(DistinguishContract.UploadView uploadView) {
        this.uploadView = uploadView;
    }

    @Override
    public void addResposeGraphTransListener(DistinguishContract.ResposeGraphTransListener resposeGraphTransListener) {
        this.resposeGraphTransListener = resposeGraphTransListener;
    }

    @Override
    public void postBrandDistingush(
            final int requestAction,
            Integer TaskID,
            Integer FrontCount,
            String QueryMode,
            String SrcImgID,
            String ImgIDs,
            String CnContent,
            String EnContent,
            String ImageCode,
            String MarkClass,
            String MarkGroup,
            String ImageQueryMode,
            String ImageAlgorithm,
            String CnQueryMode,
            String CnAlgorithm,
            String EnQueryMode,
            String EnAlgorithm,
            String CodeQueryMode,
            String CodeAlgorithm,
            String State,
            String DateType,
            String BeginDate,
            String EndDate) {
        getCommonObservable(requestAction,
                brandObservable.searchByImg(TaskID, FrontCount, QueryMode, SrcImgID, ImgIDs, CnContent, EnContent, ImageCode, MarkClass,
                        MarkGroup, ImageQueryMode, ImageAlgorithm, CnQueryMode, CnAlgorithm, EnQueryMode, EnAlgorithm, CodeQueryMode, CodeAlgorithm,
                        State, DateType, BeginDate, EndDate))
                .subscribe(new Consumer<BrandItemInfoWo>() {
                    @Override
                    public void accept(@NonNull BrandItemInfoWo brandItemInfoWo) throws Exception {
                        commonView.responseSuccess(requestAction, brandItemInfoWo);
                    }
                });

    }

    @Override
    public void postTaskID(final int requestAction) {
        getCommonObservable(requestAction, brandObservable.getTaskID(UserInfoSharePreference.getUserName(MyApplication.getContext())))
                .subscribe(new Consumer<TaskIDWo>() {
                    @Override
                    public void accept(@NonNull TaskIDWo taskIDWo) throws Exception {
                        commonView.responseSuccess(requestAction, taskIDWo);
                    }
                });
    }

    @Override
    public void postSearchResultByTaskID(int requestAction, Integer TaskID, String SortType, Integer BeginRow, Integer EndRow) {
        getCommonObservable(requestAction, brandObservable.getSearchResultByTaskID(TaskID, SortType, BeginRow, EndRow))
                .subscribe(new Consumer<BrandItemInfoWo>() {
                    @Override
                    public void accept(@NonNull BrandItemInfoWo brandItemInfoWo) throws Exception {
                        if (commonView != null){
                            commonView.responseSuccess(requestAction, brandItemInfoWo);
                        }
                        if (brandItemView != null){
                            brandItemView.responseSuccess(requestAction, brandItemInfoWo);
                        }
                    }
                });
    }

    @Override
    public void uploadImage(int requestAction, String path) {
        //** 所有上传的图片不能大于2M，必须jpg格式
        MultipartBody.Builder builder = new MultipartBody.Builder();
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), new File(path));
        builder.addFormDataPart("file", new File(path).getName(), requestBody);
        builder.setType(MultipartBody.FORM);
        getCommonObservable(requestAction, brandObservable.uploadFile(builder.build()))
                .subscribe(new Consumer<ImageWo>() {
                    @Override
                    public void accept(@NonNull ImageWo imageWo) throws Exception {
                        if (commonView != null){
                            commonView.responseSuccess(requestAction, imageWo);
                        }
                        if (uploadView != null){
                            uploadView.responseSuccess(requestAction, imageWo);
                        }
                    }
                });
    }

    @Override
    public void postUserInfo(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.userInfo(""))
                .subscribe(new Consumer<UserInfoWo>() {
                    @Override
                    public void accept(@NonNull UserInfoWo userInfoWo) throws Exception {
                        commonView.responseSuccess(requestAction, userInfoWo);
                    }
                });
    }

    @Override
    public void postUserCardList(int requestAction, Integer activeState, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, cardObservable.userCardList(activeState, pageSize, page))
                .subscribe(new Consumer<UserCardListWo>() {
                    @Override
                    public void accept(@NonNull UserCardListWo userCardListWo) throws Exception {
                        commonView.responseSuccess(requestAction, userCardListWo);
                    }
                });
    }

    @Override
    public void postTaskPayCode(int requestAction) {
        getCommonObservable(requestAction, brandObservable.getTaskPayCode(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        commonView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postTaskPay(int requestAction, Integer taskID, String validCode) {
        getCommonObservable(requestAction, brandObservable.taskPay(taskID, validCode))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        commonView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postGraphTranslation(int requestAction, String SrcImgID, Integer isAutoImgToWord, Integer isAutoImgToImgbasic, String UnionType) {
        getCommonObservable(requestAction, brandObservable.graphTranslation(SrcImgID, isAutoImgToWord, isAutoImgToImgbasic, UnionType))
                .subscribe(new Consumer<GraphTransWo>() {
                    @Override
                    public void accept(@NonNull GraphTransWo graphTransWo) throws Exception {
                        resposeGraphTransListener.resposeGraphTrans(requestAction, graphTransWo);
                    }
                });
    }
}
